﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyLibellus
{
    public partial class MantenimentSubject : Form
    {
        DataTable dtSubjects;
        SqlDataAdapter da;
        GestioSubject gs;
        public MantenimentSubject()
        {
            InitializeComponent();
            dtSubjects = DBAccess.getDataTAble("subject");
        }

        private void MantenimentSubject_Load(object sender, EventArgs e)
        {
            gridSubjects.DataSource = dtSubjects;
            SqlConnection con = DBAccess.getConnection();
            con.Open();
            da = DBAccess.getAdapter("select * from subject", con, dtSubjects);
            SqlCommandBuilder builder = new SqlCommandBuilder(da) ;
            gs = new GestioSubject();
        }

        private void btnAfegirSubject_Click(object sender, EventArgs e)
        {
            gs.S = null;
            gs.ShowDialog();
            if (gs.Aceptar)
            {

                //Modificar la fila.
                DataRow dr = dtSubjects.NewRow();
                dr["subject"] = gs.S.Nom;

                dtSubjects.Rows.Add(dr);
                da.Update(dtSubjects);
                gridSubjects.Refresh();
            }
        }

        private void btnModificarSubject_Click(object sender, EventArgs e)
        {
            //si la grid esta buida o no s'ha seleccionat cap fila no fa res.
            if (gridSubjects.SelectedRows.Count != 0 && gridSubjects.SelectedRows.Count != 0)
            {
                Subject s = new Subject();
                s.Nom = gridSubjects.SelectedRows[0].Cells[1].Value.ToString();

                gs.S = s;
                gs.ShowDialog();
                if (gs.Aceptar)
                {

                    //Modificar la fila.
                    DataRow dr = dtSubjects.Rows[gridSubjects.SelectedRows[0].Index];
                    dr["subject"] = s.Nom;


                    da.Update(dtSubjects);

                }

            }
        }

        private void btnEliminarSubject_Click(object sender, EventArgs e)
        {
            if (gridSubjects.SelectedRows.Count != 0)
            {
                if (MessageBox.Show("ESTA SEGUR D'ESBORRAR AQUEST TEMA?", "Confirmar Esborrar", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Subject s = new Subject();
                    s.Id = int.Parse(gridSubjects.SelectedRows[0].Cells[0].Value.ToString());
                    EliminarBookSubject(s);
                    int pos = gridSubjects.SelectedRows[0].Index; //posicio del subject dins la grid.
                    dtSubjects.Rows[pos].Delete();
                    da.Update(dtSubjects);
                    gridSubjects.Refresh();
                }
            }

        }

        public static void EliminarBookSubject(Subject s)
        {
            string del = "delete from bookSubject where subject_id='" + s.Id + "'";
            SqlConnection con = DBAccess.getConnection();
            con.Open();
            SqlCommand sc = new SqlCommand(del, con);
            sc.ExecuteNonQuery();
            con.Close();
        }

        private void btnSortirAplicacio_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
